<template>
  <div class="container">
    <div class="containerTop">
      <div class="topSelect">
        <el-form size="small" label-width="100px" class="rulForm">
          <el-form-item label="区域 :" class="region" label-width="50px">
            <el-cascader
              size="large"
              :options="pcaTextArr"
              v-model="region"
              :props="{ checkStrictly: true }"
              clearable
            >
            </el-cascader>
          </el-form-item>
          <el-form-item label="店铺搜索：" class="account" label-width="100px">
            <el-input
              placeholder="请输入店铺名称"
              class="selWidth"
              size="small"
              clearable
              v-model="tableForm.shopName"
              @keyup.enter.native="toSearch"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                size="small"
                @click="toSearch"
              />
            </el-input>
          </el-form-item>
          <el-button class="ResetSearch" size="small" @click="resetSearch"
            >重置</el-button
          >
        </el-form>
      </div>
      <!-- 按钮 -->
      <div class="buttonArea">
        <el-button class="mr10" size="small" type="primary" @click="goAdd"
          >新增店铺</el-button
        >
      </div>
    </div>
    <div class="center">
      <el-table :data="tableData.tableList" style="width: 100%">
         <el-table-column
          fixed
          prop="province,city,county"
          label="区域"
          header-align="center"
          align="center"
        >
        <template slot-scope="scope">
          {{scope.row.province}}{{scope.row.city}}{{scope.row.county}}
        </template>
        </el-table-column>
        <el-table-column
          fixed
          prop="address"
          label="店铺详细地址"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="shopName"
          label="店铺名称"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="introduce"
          label="店铺介绍"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="score"
          label="店铺评分"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="remark"
          label="备注"
          header-align="center"
          align="center"
        >
        </el-table-column>

        <el-table-column
          fixed
          prop="contact"
          label="联系人"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="phone"
          label="联系电话"
          header-align="center"
          align="center"
        >
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template #default="scope">
            <el-button type="text" size="small" @click="goEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="del(scope.row)"
              >删除</el-button
            >
            <el-button type="text" size="small" @click="goDetail(scope.row)"
              >详情</el-button
            >
            <!-- <el-button type="text" size="small" @click="goAddAi(scope.row)"
              >新增ai-头像</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="pageChange"
        :current-page="tableForm.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="tableForm.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        class="blockPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getShopListApi, delShopApi } from "@/api/aishop";
import { pcaTextArr } from "element-china-area-data";
export default {
  name: "aiShopIndex",
  created() {
    this.getList(); //获取所有的店铺信息
  },
  data() {
    return {
      pcaTextArr,
      region: [], //区域
      //查询所有的店铺信息
      tableForm: {
        limit: 10,
        page: 1,
        shopName: "", //店铺名称
        province: "", //省
        city: "", //市
        county: "", //县
      },
      //所有的店铺列表
      tableData: {
        tableList: [],
        total: 0,
      },
    };
  },

  methods: {
    //搜索按钮
    async toSearch() {
      this.tableForm.province = this.region[0];
      this.tableForm.city = this.region[1];
      this.tableForm.county = this.region[2];
      this.getList(); //获取所有的店铺信息
    },
    // 重置搜索项
    resetSearch() {
      this.tableForm = {
        limit: 10,
        page: 1,
        shopName: "", //店铺名称
      };
      this.region = []
    },
    //跳转到新增-店铺页面
    goAdd() {
      this.$router.push({ name: "shopAdd" });
    },
    //跳转到新增-ai头像页面
    goAddAi(row) {
      this.$router.push({ name: "addAi", params: { id: row.id } });
    },
    //跳转到编辑-店铺页面
    goEdit(row) {
      // 1.本地缓存
      sessionStorage.setItem("datatwo", JSON.stringify(row.id));
      // 2.跳转传值
      this.$router.push({ name: "shopEdit", params: { id: row.id } });
    },
    //跳转到详情-店铺页面
    goDetail(row) {
      // 1.本地缓存
      sessionStorage.setItem("datathree", JSON.stringify(row.id));
      // 2.跳转传值
      this.$router.push({ name: "shopDetail", params: { id: row.id } });
    },

    //获取所有的店铺信息
    async getList() {
      const res = await getShopListApi(this.tableForm);
      this.tableData.tableList = res.list || [];
      this.tableData.total = res.total;
    },

    //提示框按钮
    handleClose(done) {
      done();
    },

    //删除店铺
    async del(row) {
      try {
        await this.$confirm("是否删除该店铺", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        await delShopApi({ id: row.id });
        this.$message.success("删除成功");
        this.getList();
      } catch (err) {
        console.log(err);
      }
    },
    //分页
    async pageChange(page) {
      this.tableForm.page = page;
      this.getList();
    },
    //分页
    async handleSizeChange(val) {
      this.tableForm.limit = val;
      this.getList();
    },
  },
  watch:{
    $route:{
     handler() {
        this.getList(); //获取所有的店铺信息
      },
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  .center {
    margin-top: 30px;
  }
  //顶部搜索部分
  .containerTop {
    width: 100%;
    height: 130px;
    background-color: #fff;
    padding-left: 30px;
    padding-top: 20px;
    .topSelect {
      width: 100%;
      height: 50px;
      display: flex;
      font-size: 14px;
      font-weight: 600;
      .rulForm {
        width: 100%;
        display: flex;
        .account {
          margin-left: 20px;
        }
        .ResetSearch {
          margin-left: 5px;
          margin-bottom: 17px;
        }
      }
    }
    .buttonArea {
      width: 300px;
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }

  //分页样式
  .block {
    width: 100%;
    height: 80px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: end;
  }

  //上传图片样式开始
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    // background-color: blue;
    border: 1px solid #dde0e7;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 98px;
    height: 98px;
    display: block;
  }
  //上传图片样式结束
}
</style>